/**
 * Created by Ruzi on 2015/12/10.
 */

$(function() {

    $(".topo")
        .click(
        function() {

            // 1.判断服务器操作系统,ip地址,默认网关
            // var server=server_info();
            var ip_first = prompt("请输入链接外网的设备ip");
            console.log("ip_first",ip_first);


            if(ip_first!=""&&ip_first!=null){

                var re =  /^([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/;
                if(!re.test(ip_first)){
                    alert("ip地址格式不正确，请修改");
                    return false;
                }


                $
                .ajax({
                    url : "1_server_info.php",
                    type : "post",
                    dataType : "json",
                    success : function(e) {

                        var aj1 = e;
                        var sv = "操作系统" + aj1[0]["操作系统"]
                            + " 服务器网关:" + aj1[0]["服务器网关"];
                        $(".server_info").removeClass("hidden");
                        $(".server_info").text(sv);
                        if (aj1[0].success) {
                        }

                        var url = aj1[0]["服务器网关"];

                        $
                            .ajax({
                                url : "2_deviceinfo.php",
                                type : "post",
                                dataType : "json",
                                data : {
                                    url : url,
                                    community : "public"
                                },
                                success : function(e) {
                                    if (e.success) {

                                        var sysDesc = e["设备信息"]["sysDescr"];
                                        var sysName = e["设备信息"]["sysName"];
                                        // console.log("系统描述",sysDesc,"系统名称",sysName);
                                        var type = "";

                                        if (sysDesc
                                                .indexOf("H3C") >= 0) {
                                            // console.log("includes1:",sysDesc.includes("Router"));

                                            if (sysDesc
                                                    .indexOf("Router") >= 0) {

                                                // console.log("h3c
                                                // Router");
                                                type = "Router";

                                            }
                                            if (sysDesc
                                                    .indexOf("Switch") >= 0) {

                                                // console.log("h3c
                                                // switch");
                                                type = "Switch";
                                            }
                                            if (sysDesc
                                                    .indexOf("S5100-24P-EI") >= 0) {

                                                // console.log("h3c
                                                // switch");
                                                type = "h3c device";

                                            }

                                        } else if (sysDesc
                                                .indexOf("AirPort") >= 0) {
                                            // AirPort
                                            console
                                                .log(
                                                host,
                                                "AirPort");
                                            type = "AirPort";
                                        }

                                        info = {
                                            success : true,
                                            ip : url,
                                            sysType : type,
                                            sysDecs : sysDesc,
                                            sysName : sysName
                                        };

                                    } else {

                                        info = {
                                            success : true,
                                            ip : url,
                                            sysType : "",
                                            sysDecs : sysDesc,
                                            sysName : sysName
                                        };

                                    }

                                    console.log("info",
                                        info);

                                    var dv1 = info;

                                    $
                                        .ajax({
                                            url : "4_net_device.php",
                                            type : "post",
                                            dataType : "json",
                                            data : {
                                                url : url
                                            },
                                            success : function(
                                                e) {

                                                var ndv = e;

                                                var ift = "该默认网管下的所有设备ip数量:"
                                                    + ndv["网络设备下的设备信息"][0]["ip_address"].length;
                                                $(
                                                    ".info")
                                                    .removeClass(
                                                    "hidden");
                                                $(
                                                    ".info")
                                                    .text(
                                                    ift);

                                                console
                                                    .log("phase 3 complete");
                                                var all_ip = ndv["网络设备下的设备信息"][0]["ip_address"];
                                                // console.log("网络设备下的设备信息--所有信息",ndv["网络设备下的设备信息"][0]);

                                                var snmp_cap = [];// 所有设备中具备snmp功能的设备ip地址

                                                console
                                                    .log("phase 4 complete");
                                                // 5.查询网络设备下的设备信息
                                                $(".go")
                                                    .removeClass(
                                                    "hidden");

                                                var index = 0;

                                                function checkip(
                                                    i) {

                                                    $(
                                                        ".go")
                                                        .text(
                                                        "checking ip "
                                                        + all_ip[i]);

                                                    $
                                                        .ajax({
                                                            url : "5_check_topo_cap.php",
                                                            type : "post",
                                                            dataType : "json",
                                                            async : true,
                                                            data : {
                                                                url : all_ip[i]
                                                            },
                                                            success : function(
                                                                e) {

                                                                var rs1 = e;

                                                                var len = 1;

                                                                if (index > 0) {
                                                                    len = (index / all_ip.length) * 100;
                                                                    if (len > 100) {
                                                                        len = 100;
                                                                    }
                                                                    if (len < 10) {
                                                                        len = 10;
                                                                    }

                                                                }
                                                                $(
                                                                    ".go")
                                                                    .css(
                                                                    "width",
                                                                    len
                                                                    + "%");

                                                                if (rs1.success) {

                                                                    snmp_cap[snmp_cap.length] = all_ip[i];

                                                                }

                                                                index++;
                                                                if (index < all_ip.length
                                                                    && index >= 0) {
                                                                    checkip(index);
                                                                } else {

                                                                    $(
                                                                        ".go")
                                                                        .text(
                                                                        "check complete");
                                                                    console
                                                                        .log(
                                                                        "具有snmp功能的设备",
                                                                        snmp_cap);

                                                                    $(
                                                                        ".go_snmp")
                                                                        .removeClass(
                                                                        "hidden");

                                                                    $(
                                                                        ".go_snmp")
                                                                        .text(
                                                                        "具有snmp功能的设备数量:"
                                                                        + snmp_cap.length);

                                                                    // 6.判断具有snmp功能的设备
                                                                    // 具有snmp功能的设备中的交换机，路由器
                                                                    var routers = [];
                                                                    var switchs = [];
                                                                    var airports = [];
                                                                    var others = [];

                                                                    var sr = [];
                                                                    sr[0] = url;

                                                                    var ip_i = 0;

                                                                    function get_info1(
                                                                        ip_i) {

                                                                        $
                                                                            .ajax({
                                                                                url : "2_deviceinfo.php",
                                                                                type : "post",
                                                                                async : true,
                                                                                dataType : "json",
                                                                                data : {
                                                                                    url : snmp_cap[ip_i],
                                                                                    community : "public"
                                                                                },
                                                                                success : function(
                                                                                    e) {
                                                                                    if (e.success) {

                                                                                        var sysDesc = e["设备信息"]["sysDescr"];
                                                                                        var sysName = e["设备信息"]["sysName"];
                                                                                        console.log("257 系统描述",sysDesc,"系统名称",sysName);
                                                                                        var type = "";

                                                                                        if (sysDesc
                                                                                                .indexOf("H3C") >= 0) {
                                                                                            // console.log("includes1:",sysDesc.includes("Router"));

                                                                                            if (sysDesc
                                                                                                    .indexOf("Router") >= 0) {

                                                                                                // console.log("h3c
                                                                                                // Router");
                                                                                                type = "Router";

                                                                                                info = {
                                                                                                    success : true,
                                                                                                    ip : snmp_cap[ip_i],
                                                                                                    sysType : type,
                                                                                                    sysDecs : sysDesc,
                                                                                                    sysName : sysName
                                                                                                };

                                                                                                routers[routers.length] = info;

                                                                                                sr[sr.length] = snmp_cap[ip_i];

                                                                                            }
                                                                                            if (sysDesc
                                                                                                    .indexOf("Switch") >= 0) {

                                                                                                // console.log("h3c
                                                                                                // switch");
                                                                                                type = "Switch";

                                                                                                info = {
                                                                                                    success : true,
                                                                                                    ip : snmp_cap[ip_i],
                                                                                                    sysType : type,
                                                                                                    sysDecs : sysDesc,
                                                                                                    sysName : sysName
                                                                                                };

                                                                                                switchs[switchs.length] = info;
                                                                                                sr[sr.length] = snmp_cap[ip_i];

                                                                                            }
                                                                                            if (sysDesc
                                                                                                    .indexOf("S5100-24P-EI") >= 0) {

                                                                                                // console.log("h3c
                                                                                                // switch");
                                                                                                type = "h3c device";
                                                                                                info = {
                                                                                                    success : true,
                                                                                                    ip : snmp_cap[ip_i],
                                                                                                    sysType : type,
                                                                                                    sysDecs : sysDesc,
                                                                                                    sysName : sysName
                                                                                                };

                                                                                            }

                                                                                        } else if (sysDesc
                                                                                                .indexOf("AirPort") >= 0) {
                                                                                            // AirPort
                                                                                            // console.log(snmp_cap[ip_i],"AirPort");
                                                                                            type = "AirPort";

                                                                                            info = {
                                                                                                success : true,
                                                                                                ip : snmp_cap[ip_i],
                                                                                                sysType : type,
                                                                                                sysDecs : sysDesc,
                                                                                                sysName : sysName
                                                                                            };

                                                                                            airports[airports.length] = info;
                                                                                            sr[sr.length] = snmp_cap[ip_i];

                                                                                        } else {

                                                                                            info = {
                                                                                                success : true,
                                                                                                ip : snmp_cap[ip_i],
                                                                                                sysType : "",
                                                                                                sysDecs : sysDesc,
                                                                                                sysName : sysName
                                                                                            };

                                                                                            others[others.length] = info;
                                                                                            sr[sr.length] = snmp_cap[ip_i];

                                                                                        }

                                                                                    } else {

                                                                                        info = {
                                                                                            success : true,
                                                                                            ip : snmp_cap[ip_i],
                                                                                            sysType : "",
                                                                                            sysDecs : sysDesc,
                                                                                            sysName : sysName
                                                                                        };

                                                                                        others[others.length] = info;
                                                                                        sr[sr.length] = snmp_cap[ip_i];

                                                                                    }

                                                                                    ip_i++;
                                                                                    if (ip_i < snmp_cap.length
                                                                                        && ip_i >= 0) {

                                                                                        get_info1(ip_i);

                                                                                    } else {

                                                                                        var origin = ndv["网络设备下的设备信息"][0]["ip_address"];

                                                                                        console
                                                                                            .log(
                                                                                            "默认网关连接的设备下的设备信息",
                                                                                            origin);
                                                                                        console
                                                                                            .log(
                                                                                            "具有snmp功能的设备中的路由器",
                                                                                            routers);
                                                                                        console
                                                                                            .log(
                                                                                            "具有snmp功能的设备中的交换机",
                                                                                            switchs);
                                                                                        console
                                                                                            .log(
                                                                                            "具有snmp功能的设备中的无线热点",
                                                                                            airports);
                                                                                        console
                                                                                            .log(
                                                                                            "具有snmp功能的设备中的其他设备",
                                                                                            others);

                                                                                        $
                                                                                            .ajax({
                                                                                                url : "3_main_detect.php",
                                                                                                type : "post",
                                                                                                dataType : "json",
                                                                                                data : {
                                                                                                    url : url
                                                                                                },
                                                                                                success : function(
                                                                                                    e) {
                                                                                                    var origin1 = e;

                                                                                                    var origin_ip = ndv;

                                                                                                    console
                                                                                                        .log("phase 7 complete");

                                                                                                    console
                                                                                                        .log(
                                                                                                        "网关设备",
                                                                                                        origin1);
                                                                                                    console
                                                                                                        .log(
                                                                                                        "链接在网关的其他路由器和交换机信息",
                                                                                                        origin_ip);

                                                                                                    var m = [];

                                                                                                    var a = origin1.info;
                                                                                                    var b = origin_ip["网络设备下的设备信息"][0];

                                                                                                    /*
                                                                                                    for ( var i = 0; i < b["mac"].length; i++) {
                                                                                                        var mac1 = b["mac"][i];

                                                                                                        var ip1 = b["ip_address"][i];

                                                                                                        for ( var j = 0; j < a["mac"].length; j++) {
                                                                                                            var mac2 = a["mac"][j];
                                                                                                            var port1 = a["port"][j];

                                                                                                            if (mac1 == mac2) {

                                                                                                                m[m.length] = {
                                                                                                                    "mac" : mac2,
                                                                                                                    "ip" : ip1,
                                                                                                                    "port" : port1
                                                                                                                };

                                                                                                            }
                                                                                                        }

                                                                                                    }

                                                                                                    */

                                                                                                    for(var i=0;i<b["mac"].length;i++){
                                                                                                        var mac=b["mac"][i];
                                                                                                        var mtmp=mac.split(":");
                                                                                                        var mac1="";
                                                                                                        for(var p=0;p<mtmp.length;p++){
                                                                                                            var tmp_p=mtmp[p];
                                                                                                            if(mtmp[p].length==1){
                                                                                                                tmp_p="0"+tmp_p;
                                                                                                            }

                                                                                                            if(p==(mtmp.length-1)){
                                                                                                                mac1=mac1+tmp_p;
                                                                                                            }else{
                                                                                                                mac1=mac1+tmp_p+":";
                                                                                                            }
                                                                                                        }

                                                                                                        var ip1=b["ip_address"][i];

                                                                                                        for(var j=0;j<a["mac"].length;j++){
                                                                                                            var mac2=a["mac"][j];
                                                                                                            var port1=a["port"][j];

                                                                                                            if(mac1==mac2){

                                                                                                                m[m.length]={"mac":mac2,"ip":ip1,"port":port1};

                                                                                                            }
                                                                                                        }

                                                                                                    }

                                                                                                    console
                                                                                                        .log(
                                                                                                        "数据:",
                                                                                                        m);

                                                                                                    console
                                                                                                        .log(
                                                                                                        "交换机,无线ap,路由器",
                                                                                                        sr);

                                                                                                    $(
                                                                                                        ".go_snmp")
                                                                                                        .removeClass(
                                                                                                        "hidden");

                                                                                                    var source_data = [];

                                                                                                    source_data[0] = {
                                                                                                        ip : url,
                                                                                                        port : "0",
                                                                                                        name : dv1["sysName"],
                                                                                                        type : dv1["sysType"]
                                                                                                    };

                                                                                                    for ( var i = 0; i < m.length; i++) {

                                                                                                        var ip = m[i]["ip"];
                                                                                                        // router
                                                                                                        // info
                                                                                                        for ( var j = 0; j < routers.length; j++) {


                                                                                                            if (ip == routers[j]["ip"]) {

                                                                                                                console
                                                                                                                    .log(
                                                                                                                    ip,
                                                                                                                    " 对应的端口号",
                                                                                                                    m[i]["port"],
                                                                                                                    "sysName",
                                                                                                                    routers[j]["sysName"]);
                                                                                                                var text = ip
                                                                                                                    + " 对应的端口号:"
                                                                                                                    + m[i]["port"]
                                                                                                                    + " sysName:"
                                                                                                                    + routers[j]["sysName"];

                                                                                                                var tmp = "<div class=\"col-xs-12  progress \" style=\"height:30px;\">"
                                                                                                                    + text
                                                                                                                    + "</div>";

                                                                                                                source_data[source_data.length] = {
                                                                                                                    ip : ip,
                                                                                                                    port : m[i]["port"],
                                                                                                                    name : routers[j]["sysName"],
                                                                                                                    type : routers[j]["sysType"]
                                                                                                                };

                                                                                                                // $(".main").append(tmp);

                                                                                                            }
                                                                                                        }

                                                                                                        // switch
                                                                                                        // info
                                                                                                        for ( var k = 0; k < switchs.length; k++) {
                                                                                                            if (ip == switchs[k]["ip"]) {

                                                                                                                console
                                                                                                                    .log(
                                                                                                                    ip,
                                                                                                                    "对应的端口号",
                                                                                                                    m[i]["port"],
                                                                                                                    "sysName",
                                                                                                                    switchs[k]["sysName"]);

                                                                                                                var text = ip
                                                                                                                    + " 对应的端口号:"
                                                                                                                    + m[i]["port"]
                                                                                                                    + " sysName:"
                                                                                                                    + switchs[k]["sysName"];

                                                                                                                var tmp = "<div class=\"col-xs-12  progress \" style=\"height:30px;\">"
                                                                                                                    + text
                                                                                                                    + "</div>";

                                                                                                                source_data[source_data.length] = {
                                                                                                                    ip : ip,
                                                                                                                    port : m[i]["port"],
                                                                                                                    name : switchs[k]["sysName"],
                                                                                                                    type : switchs[k]["sysType"]
                                                                                                                };

                                                                                                                // $(".main").append(tmp);

                                                                                                            }
                                                                                                        }

                                                                                                        // airport
                                                                                                        // info
                                                                                                        for ( var l = 0; l < airports.length; l++) {
                                                                                                            if (ip == airports[l]["ip"]) {

                                                                                                                console
                                                                                                                    .log(
                                                                                                                    ip,
                                                                                                                    " 对应的端口号",
                                                                                                                    m[i]["port"],
                                                                                                                    "sysName",
                                                                                                                    airports[l]["sysName"]);
                                                                                                                var text = ip
                                                                                                                    + " 对应的端口号:"
                                                                                                                    + m[i]["port"]
                                                                                                                    + " sysName:"
                                                                                                                    + airports[l]["sysName"];

                                                                                                                var tmp = "<div class=\"col-xs-12  progress \" style=\"height:30px;\">"
                                                                                                                    + text
                                                                                                                    + "</div>";

                                                                                                                source_data[source_data.length] = {
                                                                                                                    ip : ip,
                                                                                                                    port : m[i]["port"],
                                                                                                                    name : airports[l]["sysName"],
                                                                                                                    type : airports[l]["sysType"]
                                                                                                                };

                                                                                                                // $(".main").append(tmp);

                                                                                                            }
                                                                                                        }

                                                                                                    }

                                                                                                    console
                                                                                                        .log(
                                                                                                        "final data:",
                                                                                                        source_data);

                                                                                                    var data = {
                                                                                                        "nodes" : []

                                                                                                    };
                                                                                                    var nodes = [];
                                                                                                    for ( var i = 0; i < source_data.length; i++) {
                                                                                                        if (source_data[i]["ip"] == ip_first
                                                                                                                .toString()) {
                                                                                                            nodes[0] = ip_first
                                                                                                                .toString()
                                                                                                            + "--"
                                                                                                            + source_data[i]["name"]
                                                                                                            + "--"
                                                                                                            + source_data[i]["type"];

                                                                                                        }
                                                                                                    }

                                                                                                    for ( var i = 0; i < source_data.length; i++) {
                                                                                                        if (source_data[i]["ip"] != ip_first
                                                                                                                .toString()) {
                                                                                                            nodes[nodes.length] = source_data[i]["ip"]
                                                                                                            + "--"
                                                                                                            + source_data[i]["port"]
                                                                                                            + "--"
                                                                                                            + source_data[i]["name"]
                                                                                                            + "--"
                                                                                                            + source_data[i]["type"];

                                                                                                        }
                                                                                                    }

                                                                                                    data["nodes"] = nodes;

                                                                                                    console
                                                                                                        .log(
                                                                                                        "final data json:",
                                                                                                        data);

                                                                                                    if (source_data.length > 0) {
                                                                                                        $
                                                                                                            .ajax({
                                                                                                                url : "op.php",
                                                                                                                type : "post",
                                                                                                                data : {
                                                                                                                    data : JSON
                                                                                                                        .stringify(data)
                                                                                                                },
                                                                                                                dataType : "json",
                                                                                                                success : function(
                                                                                                                    e) {

                                                                                                                    if (e.success) {

                                                                                                                        window
                                                                                                                            .location.href="lib/ImageC/index.html";

                                                                                                                        $(".main").addClass("hidden");
                                                                                                                        $(".pic iframe").attr("src","lib/ImageC/index.html");

                                                                                                                    }
                                                                                                                }

                                                                                                            });
                                                                                                    }

                                                                                                }
                                                                                            })

                                                                                    }

                                                                                },
                                                                                error : function(
                                                                                    e) {

                                                                                }

                                                                            });

                                                                    }
                                                                    get_info1(ip_i);

                                                                }

                                                            }
                                                        });
                                                }

                                                checkip(index);

                                            }
                                        })

                                },
                                error : function(e) {

                                }

                            });

                    }
                })
            }else{
                alert("ip地址不能为空");
            }

        });

});
